import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jinke/utils/util.dart';

import '../widgets/ItemPic.dart';
import 'logic.dart';

class EditAttractPage extends StatelessWidget {
  final logic = Get.put(EditAttractLogic());
  final state = Get.find<EditAttractLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: const BackButton(color: Colors.white),
        title: Text(
          '填写招引工作',
          style: TextStyle(
              color: Colors.white, fontSize: 17.r, fontWeight: FontWeight.bold),
        ),
        backgroundColor: ColorsUtil.blue,
        centerTitle: true,
      ),
      body: Column(
        children: [
          Expanded(
              child: Column(
            children: [
              Container(
                height: 40.r,
                color: Color(0xfff2f2f2),
                child: Row(
                  children: [
                    SizedBox(
                      width: 20.r,
                    ),
                    Icon(
                      Icons.warning_amber,
                      color: Color(0xff70b603),
                      size: 12,
                    ),
                    SizedBox(
                      width: 10.r,
                    ),
                    Text(
                      '请填写集群相关工作的记录、开展等情况。',
                      style:
                          TextStyle(color: Color(0xff70b603), fontSize: 12.sp),
                    )
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.all(15.r),
                child: BrnInputText(
                  maxHeight: 200.r,
                  minHeight: 50.r,
                  minLines: 5,
                  maxLength: 500,
                  bgColor: Colors.white,
                  autoFocus: false,
                  textString: '',
                  textInputAction: TextInputAction.newline,
                  maxHintLines: 20,
                  borderColor: Color(0xffeeeeee),
                  hint: '请输入',
                  padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                  onTextChange: (text) {
                    state.content = text;
                  },
                  onSubmit: (text) {
                    print(text);
                  },
                ),
              ),
              Align(
                alignment: Alignment.centerLeft,
                child: Container(
                  child: Text(
                    '上传相关附件：（非必填，根据实际选择上传）',
                    style: TextStyle(fontSize: 12.sp, color: Color(0xff666666)),
                  ),
                  margin: EdgeInsets.only(left: 15.r),
                ),
              ),
              GestureDetector(
                onTap: () {
                  logic.pickFiles();
                },
                child: Container(
                  height: 40.r,
                  margin: EdgeInsets.all(15.r),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(20.r),
                      border: Border.all(
                          color: const Color(
                            0xffeeeeee,
                          ),
                          width: 1),
                      color: Colors.white),
                  child: Center(
                    child: Text(
                      '选择上传文件',
                      style: TextStyle(
                          color: const Color(0xff333333), fontSize: 14.sp),
                    ),
                  ),
                ),
              ),
              GetBuilder<EditAttractLogic>(builder: (_) {
                return Container(
                  child: GridView.builder(
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 3,
                            crossAxisSpacing: 10,
                            mainAxisSpacing: 10,
                            childAspectRatio: 1),
                    itemBuilder: (context, index) {
                      return state.listImagePaths.length > index
                          ? ItemPic(index)
                          : GestureDetector(
                              child: Container(
                                child: Center(
                                  child: Column(children: [
                                    Container(
                                      child: Icon(
                                        Icons.add,
                                        size: 28,
                                        color: Color(0xffc7c7c7),
                                      ),
                                      margin: EdgeInsets.only(bottom: 10),
                                    ),
                                    Text(
                                      '最多添加9张照片',
                                      style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 12.r),
                                    )
                                  ], mainAxisSize: MainAxisSize.min),
                                ),
                                decoration: BoxDecoration(
                                    color: Color(0xfff9f9f9),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(5.r)),
                                    border: Border.all(
                                        color: Color(0xffc3c3c3), width: 0.5)),
                              ),
                              onTap: () {
                                logic.selectImages();
                              },
                            );
                    },
                    itemCount: state.listImagePaths.length == 9
                        ? 9
                        : state.listImagePaths.length + 1,
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                  ),
                  margin: EdgeInsets.all(15.r),
                );
              })
            ],
          )),
          Divider(
            height: 1,
            thickness: 1,
            color: Color(0xffeeeeee),
          ),
          Row(
            children: [
              Expanded(
                  child: GestureDetector(
                onTap: () {
                  BrnDialogManager.showConfirmDialog(context,
                      message: "暂存招引工作？",
                      cancel: '取消',
                      confirm: '确定', onCancel: () {
                    Get.back();
                    Get.back(result: true);
                  }, onConfirm: () {
                    Get.back();
                  });
                },
                child: Container(
                  height: 55.r,
                  color: Colors.white,
                  child: Center(
                    child: Text(
                      '暂存',
                      style: TextStyle(
                          color: Color(0xff333333),
                          fontSize: 15.r,
                          fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
              )),
              Expanded(
                  child: GestureDetector(
                onTap: () {
                  BrnDialogManager.showConfirmDialog(context,
                      message: "保存招引工作？",
                      cancel: '取消',
                      confirm: '确定', onCancel: () {
                    Get.back();
                  }, onConfirm: () {
                    Get.back();
                    logic.commit();
                  });
                },
                child: Container(
                  height: 55.r,
                  decoration: BoxDecoration(
                    gradient: const LinearGradient(
                        //渐变位置
                        begin: Alignment.centerLeft, //右上
                        end: Alignment.centerRight, //左下
                        stops: [0.0, 1.0], //[渐变起始点, 渐变结束点]
                        //渐变颜色[始点颜色, 结束颜色]
                        colors: [Color(0xff00A9FE), Color(0xFF0173FE)]),
                    color: Colors.white,
                  ),
                  child: Center(
                    child: Text(
                      '保存',
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 15.sp,
                          fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
              )),
            ],
          )
        ],
      ),
    );
  }
}
